/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.service.ServiceException.Argument;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.common.util.ZimbraHttpConnectionManager;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.AuthTokenException;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.servlet.ZimbraServlet;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.commons.httpclient.Cookie;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.HttpException;
/*     */ import org.apache.commons.httpclient.HttpState;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class StatsImageServlet
/*     */   extends ZimbraServlet
/*     */ {
/*  52 */   private static Log mLog = LogFactory.getLog(StatsImageServlet.class);
/*     */   private static final String IMG_NOT_AVAIL = "data_not_available.gif";
/*     */   
/*     */   public void init() throws ServletException
/*     */   {
/*  57 */     String name = getServletName();
/*  58 */     mLog.info("Servlet " + name + " starting up");
/*  59 */     super.init();
/*     */   }
/*     */   
/*     */   public void destroy() {
/*  63 */     String name = getServletName();
/*  64 */     mLog.info("Servlet " + name + " shutting down");
/*  65 */     super.destroy();
/*     */   }
/*     */   
/*     */   public void doGet(HttpServletRequest req, HttpServletResponse resp)
/*     */     throws ServletException, IOException
/*     */   {
/*  71 */     AuthToken authToken = getAdminAuthTokenFromCookie(req, resp);
/*  72 */     if (authToken == null) {
/*  73 */       return;
/*     */     }
/*  75 */     String imgName = null;
/*  76 */     InputStream is = null;
/*  77 */     boolean imgAvailable = true;
/*  78 */     boolean localServer = false;
/*  79 */     boolean systemWide = false;
/*     */     
/*  81 */     String serverAddr = "";
/*     */     
/*  83 */     String noDefaultImg = req.getParameter("nodef");
/*  84 */     boolean noDefault = false;
/*  85 */     if ((noDefaultImg != null) && (!noDefaultImg.equals("")) && (noDefaultImg.equals("1"))) {
/*  86 */       noDefault = true;
/*     */     }
/*  88 */     String reqPath = req.getRequestURI();
/*     */     
/*     */ 
/*     */     try
/*     */     {
/*  93 */       String serviceHostname = Provisioning.getInstance().getLocalServer().getAttr("zimbraServiceHostname");
/*  94 */       String logHost = Provisioning.getInstance().getConfig().getAttr("zimbraLogHostname");
/*  95 */       if (!serviceHostname.equalsIgnoreCase(logHost)) {
/*  96 */         StringBuffer url = new StringBuffer("https");
/*  97 */         url.append("://").append(logHost).append(':').append(LC.zimbra_admin_service_port.value());
/*  98 */         url.append(reqPath);
/*  99 */         String queryStr = req.getQueryString();
/* 100 */         if (queryStr != null) {
/* 101 */           url.append('?').append(queryStr);
/*     */         }
/*     */         
/* 104 */         HttpState state = new HttpState();
/*     */         try {
/* 106 */           state.addCookie(new Cookie(logHost, "ZM_ADMIN_AUTH_TOKEN", authToken.getEncoded(), "/", null, false));
/*     */         } catch (AuthTokenException ate) {
/* 108 */           throw ServiceException.PROXY_ERROR(ate, url.toString());
/*     */         }
/* 110 */         HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/* 111 */         client.setState(state);
/* 112 */         GetMethod get = new GetMethod(url.toString());
/*     */         try {
/* 114 */           int statusCode = HttpClientUtil.executeMethod(client, get);
/* 115 */           if (statusCode != 200) {
/* 116 */             throw ServiceException.RESOURCE_UNREACHABLE(get.getStatusText(), null, new ServiceException.Argument[0]);
/*     */           }
/* 118 */           resp.setContentType("image/gif");
/* 119 */           ByteUtil.copy(get.getResponseBodyAsStream(), true, resp.getOutputStream(), false); return;
/*     */         }
/*     */         catch (HttpException e) {
/* 122 */           throw ServiceException.RESOURCE_UNREACHABLE(get.getStatusText(), e, new ServiceException.Argument[0]);
/*     */         } catch (IOException e) {
/* 124 */           throw ServiceException.RESOURCE_UNREACHABLE(get.getStatusText(), e, new ServiceException.Argument[0]);
/*     */         } finally {
/* 126 */           get.releaseConnection();
/*     */         }
/*     */       }
/*     */     } catch (Exception ex) {
/* 130 */       resp.sendError(404, "Image not found");
/* 131 */       return;
/*     */     }
/*     */     try
/*     */     {
/* 135 */       if ((reqPath == null) || (reqPath.length() == 0)) {
/* 136 */         resp.sendError(400);
/* 137 */         return;
/*     */       }
/*     */       
/* 140 */       if (mLog.isDebugEnabled()) {
/* 141 */         mLog.debug("received request to:(" + reqPath + ")");
/*     */       }
/* 143 */       String[] reqParts = reqPath.split("/");
/*     */       
/* 145 */       String reqFilename = reqParts[3];
/* 146 */       imgName = LC.stats_img_folder.value() + File.separator + reqFilename;
/*     */       try {
/* 148 */         is = new FileInputStream(imgName);
/*     */       } catch (FileNotFoundException ex) {
/* 150 */         if (is != null)
/* 151 */           is.close();
/* 152 */         if (!noDefault) {
/* 153 */           imgName = LC.stats_img_folder.value() + File.separator + "data_not_available.gif";
/* 154 */           is = new FileInputStream(imgName);
/*     */         }
/*     */         else {
/* 157 */           resp.sendError(404, "Image not found");
/* 158 */           return;
/*     */         }
/*     */       }
/*     */     } catch (Exception ex) {
/* 162 */       if (is != null) {
/* 163 */         is.close();
/*     */       }
/* 165 */       resp.sendError(500, "FNF image File not found");
/* 166 */       return;
/*     */     }
/* 168 */     resp.setContentType("image/gif");
/* 169 */     ByteUtil.copy(is, true, resp.getOutputStream(), false);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/StatsImageServlet.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */